#ifndef __TASK_SCHEDULER_H__
#define __TASK_SCHEDULER_H__

#include <chrono>
#include <cstdint>
#include <functional>

#include "queue/priority_queue.h"

using TaskFunction = std::function<void()>;

enum class TaskPriority
{
    High,
    Medium,
    Low
};

enum class TaskState
{
    Created,
    Ready,
    Executing,
    Completed,
    Failed
};

struct Task
{
    Task(const TaskPriority& rhsPriority, const TaskFunction& rhsFun)
        : priority(rhsPriority), state(TaskState::Created), nextId(0), fun(rhsFun){};

    void execute()
    {
        state = TaskState::Executing;
        startTime = std::chrono::high_resolution_clock::now();
        fun();
        endTime = std::chrono::high_resolution_clock::now();

        auto duration = endTime - startTime;
        // 使用duration...

        state = TaskState::Completed;
    }

  private:
    TaskPriority priority;
    TaskState state;
    uint64_t nextId;
    TaskFunction fun;

    std::chrono::high_resolution_clock::time_point startTime;
    std::chrono::high_resolution_clock::time_point endTime;

    friend class TaskCompare;
};

class TaskCompare
{
  public:
    bool operator()(const Task& a, const Task& b) { return a.priority > b.priority; }
};

class Scheduler
{
  public:
    uint64_t addTask(Task&& task);
    void executeAllTasks();
    void clearTasks();

  private:
    PriorityQueue<Task, TaskCompare> tasks;
    uint64_t nextId = 0;
};

#endif  //__TASK_SCHEDULER_H__